

<!DOCTYPE html>
<html lang="null">
<head prefix="og: http://ogp.me/ns#">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="description" content="保存一下自己积累的技术资料">
    <title>python使用小技巧 | 黄龙天空</title>

    <!-- Open Graph -->
    <meta name="description" content="sorted&amp;#x51FD;&amp;#x6570;&amp;#x548C;list&amp;#x7C7B;&amp;#x578B;&amp;#x7684;sort&amp;#x7684;&amp;#x4F7F;&amp;#x7528;&amp;#x4E0E;&amp;#x5B57;&amp;#x9762;&amp;#x610F;&amp;#x601D;&amp;#x4E00;&amp;#x6837;&amp;#xFF0C;&amp;#x8FD9;&amp;#x4E24;&amp;#x4E2A;&amp;#x65B9;&amp;#x6CD5;&amp;#x90FD;&amp;#">
<meta property="og:type" content="article">
<meta property="og:title" content="python使用小技巧">
<meta property="og:url" content="http://huanglongtiankong.oschina.io/2017/05/17/python使用小技巧/index.html">
<meta property="og:site_name" content="黄龙天空">
<meta property="og:description" content="sorted&amp;#x51FD;&amp;#x6570;&amp;#x548C;list&amp;#x7C7B;&amp;#x578B;&amp;#x7684;sort&amp;#x7684;&amp;#x4F7F;&amp;#x7528;&amp;#x4E0E;&amp;#x5B57;&amp;#x9762;&amp;#x610F;&amp;#x601D;&amp;#x4E00;&amp;#x6837;&amp;#xFF0C;&amp;#x8FD9;&amp;#x4E24;&amp;#x4E2A;&amp;#x65B9;&amp;#x6CD5;&amp;#x90FD;&amp;#">
<meta property="og:updated_time" content="2018-05-01T11:05:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="python使用小技巧">
<meta name="twitter:description" content="sorted&amp;#x51FD;&amp;#x6570;&amp;#x548C;list&amp;#x7C7B;&amp;#x578B;&amp;#x7684;sort&amp;#x7684;&amp;#x4F7F;&amp;#x7528;&amp;#x4E0E;&amp;#x5B57;&amp;#x9762;&amp;#x610F;&amp;#x601D;&amp;#x4E00;&amp;#x6837;&amp;#xFF0C;&amp;#x8FD9;&amp;#x4E24;&amp;#x4E2A;&amp;#x65B9;&amp;#x6CD5;&amp;#x90FD;&amp;#">

    <script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "email": "mailto:1300896173@qq.com",
  "image": "undefined",
  "name": "黄龙天空",
  "url": "http://huanglongtiankong.oschina.io"
}
</script>
    <script>
    var algoliaEnabled = false;
    
</script>

    

    
    
        <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
    
    
    
        <!-- stylesheets list from config.yml -->
        
        <link rel="stylesheet" href="/css/aloha.css">
        
        <link rel="stylesheet" href="//cdn.bootcss.com/semantic-ui/2.2.4/semantic.min.css">
        
        <link rel="stylesheet" href="//cdn.bootcss.com/magnific-popup.js/1.1.0/magnific-popup.min.css">
        
    

</head>
<body id="body" style="margin-bottom: 0;" class="pushable">

    <div class="ui top fixed menu">
        <a class="item" id="menu-icon"><i class="sidebar icon"></i></a>
    </div>

    <div id="menu-sidebar" class="ui left vertical sidebar menu">

    <div id="sidebar-top">
        <div class="content">
            <h3>黄龙天空</h3>
        </div>
    </div>
    <div class="ui container sidebar-card">
    <div class="ui people shape content">
        <div class="active side">
            <div class="ui card">
                <div class="image">
                    <img class="ui medium bordered image" src="/images/avatar.jpg">
                </div>
                <div class="content">
                    <a class="header">黄龙天空</a>
                    
                </div>
                <div class="extra content">
                    <div class="ui list">
                        
                        
                        <div class="item">
                            <i class="mail icon" style="float: left"></i>
                            <div class="content">
                                <a href="mailto:1300896173@qq.com">1300896173@qq.com</a>
                            </div>
                        </div>
                        
                    </div>

                </div>
                <div class="extra content">
                    <div class="ui list">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>

    
    

    

    
    <a href="/index.html" class="item" >
        <i class="home icon"></i>
        首页
    </a>

    

    
</div>

    <div class="pusher body-content">
        <div id="content" class="ui main container">
            <!--<div class="ui one column grid">-->
                <!--<div class="column">-->
                    <!--<div class="ui main container">-->
                        <div id="post-python使用小技巧" class="ui main container article-type-post">

    

    <div class="ui divided grid">

        <div class="two column row" id="article-content">

            <div class="thirteen wide computer thirteen wide tablet sixteen wide mobile column">
                

<h1 class="ui header">
    
    python使用小技巧
</h1>



                <div class="article-inner">

                    <div class="article-entry" itemprop="articleBody">
                        
                        <h3 id="sorted&#x51FD;&#x6570;&#x548C;list&#x7C7B;&#x578B;&#x7684;sort&#x7684;&#x4F7F;&#x7528;"><a href="#sorted&#x51FD;&#x6570;&#x548C;list&#x7C7B;&#x578B;&#x7684;sort&#x7684;&#x4F7F;&#x7528;" class="headerlink" title="sorted&#x51FD;&#x6570;&#x548C;list&#x7C7B;&#x578B;&#x7684;sort&#x7684;&#x4F7F;&#x7528;"></a>sorted&#x51FD;&#x6570;&#x548C;list&#x7C7B;&#x578B;&#x7684;sort&#x7684;&#x4F7F;&#x7528;</h3><p>&#x4E0E;&#x5B57;&#x9762;&#x610F;&#x601D;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x7528;&#x4E8E;&#x6392;&#x5E8F;&#x7684;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;list.sort&#x65B9;&#x6CD5;&#x4F1A;&#x5C31;&#x5730;&#x6392;&#x5E8F;&#x5217;&#x8868;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;&#x662F;None&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4E0D;&#x4F1A;&#x7ED9;&#x4F60;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x5217;&#x8868;&#xFF0C;&#x800C;&#x662F;&#x628A;&#x539F;&#x6709;&#x7684;list&#x8FDB;&#x884C;&#x4E86;&#x6392;&#x5E8F;&#x3002;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; s = [1,5,2,6,7,3,4]
&gt;&gt;&gt; s.sort()
&gt;&gt;&gt; s
[1, 2, 3, 4, 5, 6, 7]
</code></pre><p>sorted&#x5219;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5217;&#x8868;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4EFB;&#x4F55;&#x7684;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x8FC7;&#x6700;&#x540E;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x4F1A;&#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x3002;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; s = (1,5,2,6,7,3,4)
&gt;&gt;&gt; sorted(s)
[1, 2, 3, 4, 5, 6, 7]
</code></pre><p>&#x4E0D;&#x7BA1;&#x662F;list.sort&#x65B9;&#x6CD5;&#x8FD8;&#x662F;sorted&#x51FD;&#x6570;&#xFF0C;&#x90FD;&#x6709;&#x4E24;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x3002;<br>reverse: &#x5982;&#x679C;&#x88AB;&#x8BBE;&#x5B9A;&#x4E3A;True,&#x5143;&#x7D20;&#x4F1A;&#x5012;&#x5E8F;&#x8F93;&#x51FA;&#x3002;&#x9ED8;&#x8BA4;&#x503C;&#x662F;False<br>key: &#x4E00;&#x4E2A;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F1A;&#x628A;&#x5E8F;&#x5217;&#x91CC;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5C06;&#x662F;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x4F9D;&#x8D56;&#x7684;&#x5BF9;&#x6BD4;&#x5173;&#x952E;&#x5B57;&#x3002; &#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; s = [-5,4,1,-3,2]
&gt;&gt;&gt; s.sort(reverse=True,key=abs)
&gt;&gt;&gt; s
[-5, 4, -3, 2, 1]
&gt;&gt;&gt; sorted(s, reverse=True, key=abs)
[-5, 4, -3, 2, 1]
# abs&#x662F;&#x53D6;python&#x4E2D;&#x53D6;&#x7EDD;&#x5BF9;&#x503C;&#x7684;&#x51FD;&#x6570;
</code></pre><p>&#x6392;&#x5E8F;&#x6280;&#x5DE7;</p>
<pre><code>&#x5148;&#x6309;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x6392;&#x5E8F;&#xFF0C;&#x518D;&#x6309;&#x53E6;&#x4E00;&#x4E2A;&#x6392;&#x5E8F;&#x3002;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#xFF1A;
[P1, p2, p3, ....pn].sort(key=lambda x:(x.grade, x.height))

&#x6B63;&#x8D1F;&#x6570;&#x6392;&#x5E8F;
[1,-2,10,-12,-4,-5,9,2].sort(key=lambda x:(x&lt;0, abs(x)))
</code></pre><h3 id="python&#x8FD0;&#x7B97;&#x7B26;&#x91CC;&#x4E00;&#x4E2A;&#x5F88;&#x6709;&#x8DA3;&#x7684;&#x95EE;&#x9898;"><a href="#python&#x8FD0;&#x7B97;&#x7B26;&#x91CC;&#x4E00;&#x4E2A;&#x5F88;&#x6709;&#x8DA3;&#x7684;&#x95EE;&#x9898;" class="headerlink" title="python&#x8FD0;&#x7B97;&#x7B26;&#x91CC;&#x4E00;&#x4E2A;&#x5F88;&#x6709;&#x8DA3;&#x7684;&#x95EE;&#x9898;"></a>python&#x8FD0;&#x7B97;&#x7B26;&#x91CC;&#x4E00;&#x4E2A;&#x5F88;&#x6709;&#x8DA3;&#x7684;&#x95EE;&#x9898;</h3><pre><code>print(1&lt;(2==2)) #False print(1&lt;2 ==2) #True

&#x7B2C;&#x4E00;&#x4E2A; ,&#x9996;&#x5148; 2==2 &#x7ED3;&#x679C;&#x662F; 1&#xFF0C;&#x6240;&#x4EE5; 1&lt;1 &#x662F; False
&#x7B2C;&#x4E8C;&#x4E2A;&#xFF0C;&#x4F60;&#x5148;&#x7406;&#x89E3;(1&lt;2&lt;3) is True, (4&gt;2&lt;3) is True
&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x6210;&#x5148;&#x6BD4;&#x8F83; 1&lt;2&#xFF0C;&#x518D;&#x6BD4;&#x8F83; 2==2&#xFF0C;&#x6240;&#x4EE5;&#x7ED3;&#x679C;&#x662F; True
</code></pre><h3 id="&#x5229;&#x7528;defaultdict&#x8FDB;&#x884C;&#x8BA1;&#x6570;"><a href="#&#x5229;&#x7528;defaultdict&#x8FDB;&#x884C;&#x8BA1;&#x6570;" class="headerlink" title="&#x5229;&#x7528;defaultdict&#x8FDB;&#x884C;&#x8BA1;&#x6570;"></a>&#x5229;&#x7528;defaultdict&#x8FDB;&#x884C;&#x8BA1;&#x6570;</h3><pre><code>In [3]: from collections import *

In [4]: s = &apos;mississippi&apos;

In [5]: d = defaultdict(int)

In [6]: for k in s:
...:     d[k] += 1
...:

In [7]: list(d.items())
Out[7]: [(&apos;m&apos;, 1), (&apos;i&apos;, 4), (&apos;s&apos;, 4), (&apos;p&apos;, 2)]
</code></pre>
                        
                    </div>

                    
                    
                    

                    <div class="description post-description">
    <span class="post-description-item">
        <time datetime="2017-05-17T06:03:10.000Z" itemprop="datePublished">2017-05-17</time>

    </span>
    <span class="post-description-item">
        

    <span>
        <i class="tags icon"></i>
    </span>

    
    <span class="ui tiny label">
        <a href="/tags/python/">python</a>
    </span>

    

    </span>
</div>

                </div>
                
                <div id="pagination" class="nav-web ui text container pagination">

    <div class="ui stackable two column divided grid container">
        <div class="row">
            <div class="column nav-left">
                

                <a href="/2018/06/28/yield/" class="ui tiny button">

                    <i class="angle left icon"></i>
                    
                    python生成器
                    

                </a>

                
            </div>
            <div class="column nav-right">
                

                <a href="/2017/03/04/linux命令/" class="ui tiny button">
                    
                    linux命令
                    
                    <i class="angle right icon"></i></a>
                
            </div>
        </div>
    </div>

</div>
                
            </div>

            
            <div class="three wide computer three wide tablet column">

                <div class="ui sticky article-toc" id="article-toc">
                    <!-- toc -->
                    <ol class="ui ordered list"><li><a href="#sorted函数和list类型的sort的使用"><span>sorted函数和list类型的sort的使用</span></a></li><li><a href="#python运算符里一个很有趣的问题"><span>python运算符里一个很有趣的问题</span></a></li><li><a href="#利用defaultdict进行计数"><span>利用defaultdict进行计数</span></a></li></ol>
                </div>

            </div>
            

        </div>
    </div>
</div>


                    <!--</div>-->
                <!--</div>-->
            <!--</div>-->
        </div>
        <div class="ui vertical footer segment">
    <div class="ui center aligned container">
        <div class="ui inverted section divider"></div>
        &copy; 2018 <a href="/">黄龙天空</a>,
        Powered by <a href="https://github.com/henryhuang/hexo-theme-aloha" target="_blank">Aloha</a> and <a
                href="http://hexo.io/" target="_blank">Hexo</a>.
    </div>
</div>

    </div>




<!-- scripts list from theme config.yml -->

<script src="//cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>

<script src="//cdn.bootcss.com/semantic-ui/2.2.4/semantic.min.js"></script>

<script src="//cdn.bootcss.com/algoliasearch/3.18.1/algoliasearch.min.js"></script>

<script src="//cdn.bootcss.com/algoliasearch-helper-js/2.13.0/algoliasearch.helper.min.js"></script>

<script src="/js/semantic-ui-algolia.js"></script>

<script src="/js/aloha-events.js"></script>



</body>
</html>
